<?php
namespace app\controller\model\apis\service;

use app\BaseController;
use app\controller\model;
use GuzzleHttp\Client;
use think\facade\Db;
class Bochuang extends BaseController
{
	public $urlTest="";
	public $develop=true;
	public $apiUrl="";
	protected $url='',$appKey,$secret,$serviceCode,$tables,$token,$iv,$key;
	public $supplier;
	public $data,$method;
	protected $header=["Content-Type" => "application/json"];
    public function __construct($service=[]){
	    $this->tables = new model\Tables();
		$this->start($service);
    }
	function setSuccess($data=[]){
		//结算完成后，鲸车惠通知到供应商
		if(!isset($data['sign']))return false;
		$sign = $data['sign'];
		unset($data['sign']);
		$this->data['data']=$data;
		$this->mkSign(true);
		if($sign == $data['sign']){
			return true;
		}
		return false;
	}
	public function setD0($act)
    {
        $this->method = "openIncrementSettle";
        $this->setData();
        $this->data['data'] = json_encode([
            'ypMerchantNo' => $act
        ]);
        $this->mkSign();
        return $this->sendUrl('merchant/openIncrementSettle');
    }
	function getPayMoney(){
		//查询指定易宝商户号商户余额
		$this->method = 'queryYopBalance';
		$this->setData();
		$this->data['data']=json_encode(['ypMerchantNo'=>$this->serviceCode]);
		$this->mkSign();
		//不支持测试环境
//		echo json_encode($this->data);
//		exit();
		return $this->sendUrl('merchant/queryYopBalance');
	}
	protected function setData(){
		$this->data=[
			'appKey'=>$this->appKey,
			'method'=>$this->method,
		];
	}
	protected function start($service=[]){
		$this->service = $service;
		$this->apiUrl = 'https://api.jches.com/api/supplier/open';
		$this->urlTest = 'https://dev.jches.com/api/supplier/open';
		if(isset($this->service['develop'])){
			if($this->service['develop']==-1) $this->develop = false;
		}
		$this->develop=false;
		!$this->develop?$this->url = $this->apiUrl:$this->url = $this->urlTest;
		$this->url = trim($this->url,"/")."/";
		$this->appKey = 'YTL';
		$this->secret = '49ad5e8c026046f881c5cb1c0158e19b';
		$this->key = substr($this->secret,0,16);
		$this->iv = substr($this->secret,-16);
		$this->serviceCode = $service['shopNo'] ?? '';
	}
	protected function mkSign($re=false){
		if($re==false){
			$this->data['timeStamp'] = "".time();
			$this->data['data']=$this->aesEncrypt($this->data['data']);
		}
		$str = $this->data['data'].$this->data['method'].$this->data['timeStamp'];
		$this->data['sign'] = md5(strtoupper($this->secret.$this->appKey.$str));
	}
	function aesEncrypt($plaintext){
		$cipher_algo = 'AES-128-CBC';
		$ciphertext = openssl_encrypt($plaintext, $cipher_algo, $this->key, 0, $this->iv);
		if ($ciphertext === false) {
			return false;
		}
		return $ciphertext;
	}
	protected function sendUrl($pra=''){
		if($pra=='') return [];
		$curl = curl_init();
		curl_setopt_array($curl, array(
			CURLOPT_URL => $this->url.$pra,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_POSTFIELDS =>json_encode($this->data),
			CURLOPT_HTTPHEADER => array(
				'Content-Type: application/json'
			),
		));
		$response = curl_exec($curl);
		curl_close($curl);
		return json_decode($response,true);
	}
}
?>